package com.ddwl.goods.dao.bargain;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.mapper.bargain.BargainOrderMapper;
import com.ddwl.goods.model.bargain.BargainOrder;
import com.ddwl.schema.bo.goods.bargain.BargainOrderPageBo;
import com.ddwl.schema.bo.goods.bargain.MyBargainOrderPageBo;
import com.ddwl.schema.util.DateUtil;
import com.ddwl.schema.vo.goods.bargain.BargainOrderSuccessVo;
import com.ddwl.schema.vo.goods.bargain.BargainOrderVo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/10/26 9:46
 */
@Repository
public class BargainOrderDao extends TkDao<BargainOrderMapper, BargainOrder> {

    @Resource
    private BargainOrderMapper bargainOrderMapper;

    public List<BargainOrder> getList(BargainOrderPageBo param) {
        Example example = getExample();
        example.and().andEqualTo("userId", param.getUserId())
                .andEqualTo("activityId",param.getActivityId())
                .andEqualTo("status", param.getStatus())
                .andLike("nickname", MySqlUtil.rightLike(param.getNickname()));
        return bargainOrderMapper.selectByExample(example);
    }

    public List<BargainOrder> getByActivityIdAndUserId(String activityId, String userId) {
        List<Integer> list = new ArrayList<>();
        list.add(GoodsEnum.BargainOrderStatusEnum.PROCESSING.getCode());
        list.add(GoodsEnum.BargainOrderStatusEnum.THRESHOLD_BUY.getCode());
        list.add(GoodsEnum.BargainOrderStatusEnum.SUCCESS.getCode());
        Example example=getExample();
        example.and().andEqualTo("userId", userId)
                .andEqualTo("activityId", activityId)
                .andIn("status", list);
        return bargainOrderMapper.selectByExample(example);
    }

    public List<BargainOrderVo> myList(MyBargainOrderPageBo param) {
        return bargainOrderMapper.myList(param.getUserId());
    }

    public BargainOrderSuccessVo getTodaySuccess(){
        Instant startTime = DateUtil.getTimesMorning();
        Instant endTime = Instant.now();
        return bargainOrderMapper.getTodaySuccess(startTime, endTime);
    }

    public BargainOrderVo getDetailById(String bargainOrderId) {
        return bargainOrderMapper.getDetailById(bargainOrderId);
    }

    public void changeExpiredStatus() {
        bargainOrderMapper.changeExpiredStatus();
    }

    public List<BargainOrder> findListByActivityIds(List<String> activityIds) {
        Example example = getExample();
        example.and().andIn("activityId", activityIds);
        return bargainOrderMapper.selectByExample(example);
    }

    public List<BargainOrder> findInvalidOrder() {
        Example example = getExample();
        example.and().andLessThanOrEqualTo("expireDate", Instant.now())
                .andEqualTo("status", GoodsEnum.BargainOrderStatusEnum.PROCESSING.getCode());
        example.or().andLessThanOrEqualTo("payExpireDate", Instant.now())
                .andEqualTo("status", GoodsEnum.BargainOrderStatusEnum.THRESHOLD_BUY.getCode())
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return bargainOrderMapper.selectByExample(example);
    }

    public void batchUpdateStatus(List<String> list) {
        Example example = getExample();
        example.and().andIn("id", list);
        BargainOrder bo = new BargainOrder();
        bo.setStatus(GoodsEnum.BargainOrderStatusEnum.FAILURE.getCode());
        bargainOrderMapper.updateByExampleSelective(bo, example);
    }
}
